'''
Created on Nov 13, 2012

@author: kafkef
'''

import sqlite3 as lite

from PyQt4.QtGui import *
from PyQt4 import QtCore
from PyQt4.QtCore import QObject, SIGNAL, pyqtSignal

from dexen.system.client import db
from dexen.system.client.controller.db_syncher import DBSyncher
from dexen.system.client.proxy import ServerProxy, JobProxy

class JobSyncher(QObject):
    
    server_info_initialized = pyqtSignal(list) # list of db.ServerInfo
    
    def __init__(self):
        super(JobSyncher, self).__init__()
        self.svr_proxies = {} # svr_info : ServerProxy
        self.cur_proxy = None # ServerProxy
    
    def on_open(self):
        self.db_syncher = DBSyncher()
        svr_info_list = self.db_syncher.get_server_info()
        for svr_info in svr_info_list:
            assert isinstance(svr_info, db.ServerInfo)
            svr_info.disconnect()
        # Emit signal
        self.server_info_initialized.emit(svr_info_list)
        # Retrieve actions from DB
        for svr_info in svr_info_list:
            assert isinstance(svr_info, db.ServerInfo)
            svr_proxy = self.get_proxy(svr_info)
            assert isinstance(svr_proxy, ServerProxy)
            for job_id, job_info in svr_info.get_jobs().items():
                actions = self.db_syncher.get_actions(job_info)
                # Process actions in the respective JobProxy
                job_proxy = svr_proxy.get_job_proxy(job_info)
                assert isinstance(job_proxy, JobProxy)
                job_proxy.process_actions(actions)
                # Emit signal
                self.emit(SIGNAL("actions_initialized"), job_info, actions)
                self.emit(SIGNAL("inds_initialized"), job_info, job_proxy.get_inds())

    def on_start(self):
        self.db_syncher = DBSyncher()

    def get_proxy(self, svr_info):
        if not self.svr_proxies.has_key(svr_info):
            self.svr_proxies[svr_info] = ServerProxy()
        return self.svr_proxies[svr_info]
    
    def on_connect(self, svr_info):
        print "JobSyncher: on_connected to:", svr_info.get_name()
        self.cur_proxy = self.get_proxy(svr_info)
        self.cur_proxy.connect(svr_info.get_name())
        self.svr_info = svr_info
        # Persist to DB
        self.db_syncher.persist_server_info(self.svr_info)
        self.svr_id = self.db_syncher.get_svr_id(self.svr_info)

    def on_disconnect(self):
        print "JobSyncher: on_disconnected"
        #self.cur_proxy.disconnect()
        self.emit(SIGNAL("server_disconnected"), self.svr_info)

    def on_get_actions(self, job_info):
        try:
            assert isinstance(job_info, db.JobInfo)
            job_id = job_info.get_id()
            job_proxy = self.cur_proxy.get_job_proxy(job_info)
            assert isinstance(job_proxy, JobProxy)
            # Retrieves new actions from the server since the last update
            job_proxy.refresh()
            actions = job_proxy.get_recent_actions()
            if len(actions) > 0:
                # Emit signal
                self.emit(SIGNAL("actions_received"), job_info, actions)
                self.emit(SIGNAL("inds_updated"), job_info, job_proxy.get_inds())
                self.db_syncher.persist_actions(job_info, actions)
        except EOFError:
            self.on_disconnected()
        #except:
        #    print "Exception in JobSyncher:on_get_actions"
        #    pass

